<template>
  <a-card style="width: 100%">

    <a-form :form="form">
      <p>初试成绩查询网址：<a @click="pageTo" >http://gs2.zzu.edu.cn/srch/master.html</a></p>

      <h3>复核前成绩查询时间</h3>
      <a-row :gutter="48">
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="开始时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-date-picker
              :disabledDate="disabledStartDate"
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              v-model="startValue"
              placeholder="请选择时间"
              @openChange="handleStartOpenChange"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="结束时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-date-picker
              :disabledDate="disabledEndDate"
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择时间"
              v-model="endValue"
              @openChange="handleEndOpenChange"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <h3>复核后查询成绩时间</h3>
      <a-row :gutter="48">
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="开始时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-date-picker
              :disabledDate="disabledStartDate2"
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              v-model="startValue2"
              placeholder="请选择时间"
              @openChange="handleStartOpenChange2"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="{span:24}" :lg="{span:12}">
          <a-form-item
            label="结束时间"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-date-picker
              :disabledDate="disabledEndDate2"
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择时间"
              v-model="endValue2"
              @openChange="handleEndOpenChange2"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row style="text-align: center" > <a-button type="primary" @click="handleSave">保存</a-button></a-row>
    </a-form>
  </a-card>
</template>
<script>
  import moment from 'moment'
  // import { mapState } from 'vuex'

  export default {
    name: 'SetDate',
    data() {
      return {
        startValue: null,
        endValue: null,
        startValue2: null,
        endValue2: null,
        endOpen: false,
      endOpen2: false
    }
  },
  watch: {
    startValue (val) {
      console.log('startValue', val)
    },
    endValue (val) {
      console.log('endValue', val)
    },
    startValue2 (val) {
      console.log('startValue', val)
    },
    endValue2 (val) {
      console.log('endValue', val)
    },
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
     this.getCurrentTime()
  },
  methods: {
    pageTo(){
      window.open('http://gs2.zzu.edu.cn/srch/master.html')
    },
    // 计算最低开始时间
    disabledStartDate (startValue) {
      const endValue = this.endValue
      if (!startValue || !endValue) {
        return false
      }
      return startValue.valueOf() > endValue.valueOf()
    },
    disabledStartDate2 (startValue2) {
      const endValue2 = this.endValue2
      if (!startValue2 || !endValue2) {
        return false
      }
      return startValue2.valueOf() > endValue2.valueOf()
    },
    // 计算最迟结束时间
    disabledEndDate (endValue) {
      const startValue = this.startValue
      if (!endValue || !startValue) {
        return false
      }
      return startValue.valueOf() >= endValue.valueOf()
    },
    disabledEndDate2 (endValue2) {
      const startValue2 = this.startValue2
      if (!endValue2 || !startValue2) {
        return false
      }
      return startValue2.valueOf() >= endValue2.valueOf()
    },
    // 开始时间
    handleStartOpenChange (open) {
      if (!open) {
        this.endOpen = true
      }
    },
    handleStartOpenChange2 (open2) {
      if (!open2) {
        this.endOpen2 = true
      }
    },
    // 结束时间
    handleEndOpenChange (open) {
      this.endOpen = open
    },
    handleEndOpenChange2 (open2) {
      this.endOpen2 = open2
    },
    // 获取当前时间
    getCurrentTime(){
      this.$api.recruitstat.resultPlanGetTime().then( res => {
             if(res.data && res.data.length){
               for(var i=0; i<res.data.length;i++){
                 if(res.data[i].gnmklx == '0006'){
                     this.startValue =moment(res.data[i].qssj);
                     this.endValue = moment(res.data[i].jssj);
                  }
                  if(res.data[i].gnmklx == '0007'){
                      this.startValue2 =moment(res.data[i].qssj);
                      this.endValue2 = moment(res.data[i].jssj);
                   }
               }

             }
      })
    },
    // 保存时进行时间转换
    getFormateTime(time){
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 保存操作
    handleSave () {
      if(this.startValue == null || this.endValue == null){
        this.$message.warning("请选择临时成绩查询时间")
        return
      }
      if(this.startValue2 == null || this.endValue2 == null){
        this.$message.warning("请选择正式查询成绩时间")
        return
      }
      var params = {'qssj':this.getFormateTime(this.startValue),'jssj':this.getFormateTime(this.endValue)}
      var params2 = {'qssj':this.getFormateTime(this.startValue2),'jssj':this.getFormateTime(this.endValue2)}
      this.$api.recruitstat.shortTimeSet(params).then( res => {
        this.$api.recruitstat.formalTimeSet(params2).then( res => {
            this.getCurrentTime()
            this.$message.success(res.msg)
        })
      })
    }
  }
}
</script>
